package com.touchy.touchpad;


import java.util.List;
 
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
 
/**
 * Android Accelerometer Sensor Manager Archetype
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */
public class AccelerometerManager {

    private static float threshold     = 0.2f;
    private static int interval     = 1000;
 
    private static Sensor sensor;
    private static SensorManager sensorManager;
    
    private static AccelerometerListener listener;
 
    private static Boolean supported;
    private static boolean running = false;
    
//    private static float scale1;
    private static float scale2;
    private static float scale3;
    
    
    
    

    
    
    private static float[] coordinates;

    public static float[] getCoordiantes(){
    	return coordinates;
    }
    
    
    public static boolean isListening() {
        return running;
    }

    public static void stopListening() {
        running = false;
        try{
            if(sensorManager != null && sensorEventListener != null){
                sensorManager.unregisterListener(sensorEventListener);
            }
        } catch (Exception e) {}
    }

    public static boolean isSupported(){
        if(supported == null){
            if (Touchpad.getContext() != null){
                sensorManager = (SensorManager) Touchpad.getContext().getSystemService(Context.SENSOR_SERVICE);
                List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
                supported = new Boolean(sensors.size() > 0);
            } else {
                supported = Boolean.FALSE;
            }
        }
        return supported;
    }

    public static void configure(int threshold, int interval) {
        AccelerometerManager.threshold = threshold;
        AccelerometerManager.interval = interval;
        
    }

    public static void startListening(AccelerometerListener accelerometerListener){
        sensorManager = (SensorManager) Touchpad.getContext().getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> sensors = sensorManager.getSensorList(
                Sensor.TYPE_ACCELEROMETER);
        if (sensors.size() > 0) {
            sensor = sensors.get(0);
            running = sensorManager.registerListener(
                    sensorEventListener, sensor, 
                    SensorManager.SENSOR_DELAY_GAME);
            listener = accelerometerListener;
        }
    }

    public static void startListening(AccelerometerListener accelerometerListener, int threshold, int interval) {
        configure(threshold, interval);
        startListening(accelerometerListener);
    }
 
    private static SensorEventListener sensorEventListener = new SensorEventListener() {
         
        private float x = 0;
        private float y = 0;
        private float z = 0;
        
        private float xL = 0;
        private float yL = 0;
        private float zL = 0;
        
        private float xf, yf, zf;

        private long now = 0;
        private float timeDiff = 0;
        private long lastUpdate = 0;
 
        public void onAccuracyChanged(Sensor sensor, int accuracy) {}
 
        public void onSensorChanged(SensorEvent event) {
        	now = event.timestamp;
        	
//        	scale1 = (float) 50000.0;
        	scale2 = (float)88.8;
        	scale3 = (float)1;
            x = (event.values[0]);
            y = (event.values[1]);
            z = (event.values[2]);
            
            if(-0.1 < x && x < 0.1){
            	x = 0;
            }
            if(-0.1 < z && y < 0.1){
            	y = 0;
            }
            if(-0.1 < z && z < 0.1){
            	z = 0;
            }
            
            if (lastUpdate == 0) {
                lastUpdate = now;
                xL = x;
                yL = y;
                zL = z;
            }
            else {
            	 timeDiff = now - lastUpdate;
            	 if(timeDiff > 0){
            		 xf = (x - xL);
            		 yf = (y - yL);
            		 zf = (z - zL);
            		
            	 }
            	 xL = x;
            	 yL = y;
            	 zL = z;
            	 lastUpdate = now;
            }
            /*
            timeDiff = timeDiff / scale1;
            timeDiff = timeDiff / scale2;
            
            xD = (xA/2)*timeDiff*timeDiff;
            yD = (yA/2)*timeDiff*timeDiff;
            zD = (zA/2)*timeDiff*timeDiff;
            
            xD = xD*scale3;
            yD = yD*scale3;
            zD = zD*scale3;
            */
            
            xf = xf*scale3;
            yf = yf*scale3;
            zf = zf*scale3;
            
            xf = xf*scale2;
            yf = yf*scale2;
            zf = zf*scale2;
            
            xf = -xf;
            
            listener.onDisplacementChanged(xf, yf, zf, timeDiff);
        }
    };
 
}